﻿using Avatar;
using ChinaUnicom.ViewModels;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace ChinaUnicom.Views
{
    public partial class LoginPage : PhoneApplicationPage
    {
        public LoginPage()
        {
            InitializeComponent();

            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            LoginContext context = new LoginContext();
            context.PropertyChanged += OnContextPropertyChanged;
            DataContext = context;

            passwordHint.Visibility = System.Windows.Visibility.Visible;
        }

        void OnContextPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            LoginContext context = sender as LoginContext;

            if (e.PropertyName.IgnoreCaseEquals("password"))
            {
                if (context.Password.IsNullOrEmpty())
                {
                    passwordHint.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    passwordHint.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        #region password textbox controll

        private void OnPasswordGotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            passwordHint.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void OnPasswordLostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (password.Password.IsNullOrEmpty())
            {
                passwordHint.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void OnPasswordHintTaped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            password.Focus();
        }

        private void OnMobileChanged(object sender, TextChangedEventArgs e)
        {
            PhoneTextBox textBox = sender as PhoneTextBox;
            if (textBox.Text.Length == 11)
            {
                password.Focus();
            }
        }

        private void OnCodeChanged(object sender, TextChangedEventArgs e)
        {
            PhoneTextBox textBox = sender as PhoneTextBox;
            if (textBox.Text.Length == 4)
            {
                this.Focus();
            }
        }

        #endregion

    }
}